package com.example.framework.activity;

import com.example.framework.R;
import com.example.framework.base.BaseActivity;
import com.example.framework.util.SqliteDBHelper;
import com.example.framework.view.CustomImageView;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;

public class Activity1 extends BaseActivity implements OnClickListener
{
	private CustomImageView imageView;
	private String string;
	private SqliteDBHelper sqliteDatabaseHelper;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity1);
		Log.i("lala", "Activity1:----->onCreate");
		sqliteDatabaseHelper = new SqliteDBHelper(this);

		findViewById(R.id.button0).setOnClickListener(this);
		findViewById(R.id.button1).setOnClickListener(this);
		findViewById(R.id.button2).setOnClickListener(this);
		findViewById(R.id.button3).setOnClickListener(this);
		findViewById(R.id.button4).setOnClickListener(this);
		findViewById(R.id.button5).setOnClickListener(this);
		findViewById(R.id.button6).setOnClickListener(this);
		imageView = (CustomImageView) findViewById(R.id.img);
	}

	@Override
	protected void onStart()
	{
		// TODO Auto-generated method stub
		super.onStart();
		Log.i("lala", "Activity1:----->onStart");
	}

	@Override
	protected void onRestart()
	{
		// TODO Auto-generated method stub
		super.onRestart();
		Log.i("lala", "Activity1:----->onRestart");
	}

	@Override
	protected void onResume()
	{
		// TODO Auto-generated method stub
		super.onResume();
		Log.i("lala", "Activity1:----->onResume");
	}

	@Override
	protected void onPause()
	{
		// TODO Auto-generated method stub
		super.onPause();
		Log.i("lala", "Activity1:----->onPause");
	}

	@Override
	protected void onStop()
	{
		// TODO Auto-generated method stub
		super.onStop();
		Log.i("lala", "Activity1:----->onStop");
	}

	@Override
	protected void onDestroy()
	{
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.i("lala", "Activity1:----->onDestroy");
		sqliteDatabaseHelper.closeDatabase();
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
			case R.id.button0:
				ContentValues contentValues = new ContentValues();
				contentValues.put("name", "xiaoming");
				if (SqliteDBHelper.INSERT_ERROR != sqliteDatabaseHelper.insert("test", contentValues))
				{
					Cursor c = sqliteDatabaseHelper.query("test", null, null, null);
					String username = null;
					String password = null;
					String result = null;
					while (c.moveToNext())
					{
						username = c.getString(c.getColumnIndex("id"));
						password = c.getString(c.getColumnIndex("name"));
						result += username + password + "/";
					}
					c.close();
					showToast(result);
				}
				else
				{
					showToast("failed");
				}
				break;
			case R.id.button1:
				showToast("deleted "+sqliteDatabaseHelper.delete("test", null, null));
				break;
			case R.id.button2:
				ContentValues cv = new ContentValues();
				cv.put("name", "xiaohong");
				showToast("update "+sqliteDatabaseHelper.update("test", cv, null, null));
				break;
			case R.id.button3:
				Cursor c = sqliteDatabaseHelper.query("test", null, null, null);
				String username = null;
				String password = null;
				String result = null;
				while (c.moveToNext())
				{
					username = c.getString(c.getColumnIndex("id"));
					password = c.getString(c.getColumnIndex("name"));
					result += username + password + "/";
				}
				c.close();
				showToast(result);
				break;
			case R.id.button4:
				sqliteDatabaseHelper.createTable("users", new String[]{"id","name"}, new String[]{"INTEGER PRIMARY KEY AUTOINCREMENT","varchar(50)"});
				break;
			case R.id.button5:
				if (sqliteDatabaseHelper.deleteDatabase())
				{
					showToast("db has deleted.");
				}
				break;
			case R.id.button6:
				imageView.setImageUrl("http://192.168.0.79:8088/a.png");
				break;
			default:
				break;
		}
	}
}
